package View;

import Hander.LenderViewHander;
import entity.TableDTO;
import ext.MainViewTable;
import ext.MainViewTableModel;
import util.DimensUtil;
import util.GetsTablefieldsUtil;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.Border;
import java.awt.Color;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.Vector;

public class LeaderView extends JFrame {
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    //角色
    public String role="DepartmentHead";
    //定义布局模式
    SpringLayout springLayout=new SpringLayout();
    //创建视图组件
    JPanel LeftPanel=new JPanel();
    JPanel RightUpPanel=new JPanel();
    public JScrollPane jScrollPane;
    public JButton jButton1=new JButton("所有人员信息");
    public JButton jButton2=new JButton("所有部门信息");
    public JButton jButton3=new JButton("所有项目信息");
    public JButton jButton4=new JButton("项目获奖信息");
    public JButton jButton5=new JButton("项目人员信息");//
    public JButton jButton8=new JButton("科研成果信息");//对成果表进行增删改

    public JButton addBtn=new JButton("添加");
    public JButton updataBtn=new JButton("修改");
    public JButton deteleBtn=new JButton("删除");

    public JLabel jLabel1=new JLabel("部门编号:");
    public JTextField searchText1=new JTextField(15);

    public JLabel jLabel2=new JLabel("项目编号:");
    public JTextField searchText2=new JTextField(15);

    public JLabel jLabel3=new JLabel("成果编号:");
    public JTextField searchText3=new JTextField(15);

    public JButton searchBtn=new JButton("查询");

    Container contentPane;
    //创建表格
    MainViewTable mainViewTable=new MainViewTable();
    //pageNew：当前页是第几页
    private int pagrNew=1;
    //pageSize：每一页都多少条记录
    private int pageSize=10;
    //创建组件监听
    public LenderViewHander lenderViewHander;
    // 创建边框
    public Border border= BorderFactory.createLineBorder(Color.GRAY, 2);

    public LeaderView(String Title){
        super(Title);
        //初始化边框
        RightUpPanel.setBorder(border);
        contentPane=getContentPane();
        contentPane.setLayout(springLayout);
        URL imgUrl = LeaderView.class.getResource("/Image/R.png");
        setIconImage(new ImageIcon(imgUrl).getImage());
        Rectangle bounds = DimensUtil.getBounds();//矩形
        //窗体的大小：
        System.out.println(bounds.height+" "+bounds.width);


        lenderViewHander=new LenderViewHander(this);

        //这两个是JPanel里面的组件布局
        jScrollPaneLayout();
        LeftPanelLayout();
        RightUpPanelLayout();

        Layout();
        // 根据屏幕大小设计主界面大小
        setBounds(bounds);
        //设置窗体完全充满屏幕大小
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        //设置contentPane窗体信息！
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        setVisible(true);
    }

    private void RightUpPanelLayout() {
        RightUpPanel.setLayout(new FlowLayout(FlowLayout.LEFT,2,5));
        addBtn.addActionListener(lenderViewHander);
        deteleBtn.addActionListener(lenderViewHander);
        updataBtn.addActionListener(lenderViewHander);
        searchBtn.addActionListener(lenderViewHander);

        RightUpPanel.add(addBtn);
        RightUpPanel.add(deteleBtn);
        RightUpPanel.add(updataBtn);
        RightUpPanel.add(jLabel1);
        RightUpPanel.add(searchText1);
        RightUpPanel.add(jLabel2);
        RightUpPanel.add(searchText2);
        RightUpPanel.add(jLabel3);
        RightUpPanel.add(searchText3);
        RightUpPanel.add(searchBtn);
    }
    private void Layout() {
        //885 1493
        LeftPanel.setPreferredSize(new Dimension(150, 885));
        RightUpPanel.setPreferredSize(new Dimension(1340, 40));
        jScrollPane.getViewport().setPreferredSize(new Dimension(1336, 845));

        springLayout.putConstraint(SpringLayout.WEST,LeftPanel,1,SpringLayout.WEST,contentPane);
        springLayout.putConstraint(SpringLayout.NORTH,LeftPanel,1,SpringLayout.NORTH,contentPane);

        springLayout.putConstraint(SpringLayout.WEST,RightUpPanel,1,SpringLayout.EAST,LeftPanel);
        springLayout.putConstraint(SpringLayout.NORTH,RightUpPanel,0,SpringLayout.NORTH,LeftPanel);

        springLayout.putConstraint(SpringLayout.WEST,jScrollPane,1,SpringLayout.EAST,LeftPanel);
        springLayout.putConstraint(SpringLayout.NORTH,jScrollPane,-1,SpringLayout.SOUTH,RightUpPanel);

        contentPane.add(LeftPanel);
        contentPane.add(RightUpPanel);
        contentPane.add(jScrollPane);
    }

    public void reloadTable(String tablename) {
        TableDTO dto=lenderViewHander.gettableDTO();
        Vector<String> columns=GetsTablefieldsUtil.getAllcolumns(tablename);
        MainViewTableModel.updataModel(dto.getData(),columns);
        mainViewTable.renderRule2(columns);
    }
    private void jScrollPaneLayout() {
        //把数据库的表格数据放进二维数组里面
        TableDTO dto = lenderViewHander.gettableDTO();
        //把二维数组加载到表格对象里面
        MainViewTableModel mainViewTableModel= MainViewTableModel.assembleModel(dto.getData());
        mainViewTable.setDataModel(mainViewTableModel);
        mainViewTable.renderRule();
        jScrollPane=new JScrollPane(mainViewTable);
        jScrollPane.setBorder(border);
    }
    private void LeftPanelLayout() {
        LeftPanel.setBorder(border);
        jButton1.addActionListener(lenderViewHander);
        jButton2.addActionListener(lenderViewHander);
        jButton3.addActionListener(lenderViewHander);
        jButton4.addActionListener(lenderViewHander);
        jButton5.addActionListener(lenderViewHander);
        jButton8.addActionListener(lenderViewHander);
        LeftPanel.add(jButton1);
        LeftPanel.add(jButton2);
        LeftPanel.add(jButton3);
        LeftPanel.add(jButton8);
        LeftPanel.add(jButton4);
        LeftPanel.add(jButton5);

    }

    public String[] getSelectedStudentIds(){
        int[] selectedRows = mainViewTable.getSelectedRows();
        String[] ids=new String[selectedRows.length];
        for (int i = 0; i < selectedRows.length; i++) {
            int Row = selectedRows[i];
            Object valueAt = mainViewTable.getValueAt(Row, 0);//获取学生表第一个字段
            ids[i]=valueAt.toString();
        }
        for (int i = 0; i < selectedRows.length; i++) {
            System.out.println("_________"+ids[i]);
        }
        return ids;
    }

    public static void main(String[] args) {
        LeaderView leaderView=new LeaderView("项目负责人");
        leaderView.addBtn.setVisible(false);
        leaderView.updataBtn.setVisible(false);
        leaderView.deteleBtn.setVisible(false);
    }
}
